<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: '标题'
    },
    unit: {
      type: String,
      default: ''
    },
    chartData: {
      type: Object,
      default: function(){
        return {}
      }
    },
    color: {
      type:Array,
      default: function() {
        return ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']
      }
    }
  },
  data() {
    return {
      chart: null,
      datas: {}
    }
  },
  watch: {
   chartData(newV,oldV) {
     // do something
     this.$nextTick (() => {
       this.initChart();
     })
    } 
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose();
    this.chart = null;
    this.datas = {};
  },
  methods: {
    update() {
      console.log(this.chartData)
    },
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id))
     let chartData = this.chartData;
     this.chart.setOption({
        backgroundColor:"#fff",
        title: {
            left: 'center',
            text: this.title,
            top: '10px'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b} : {c}  '
        },
        color: this.color,
        grid: {
          top:"15%",
          left: '10%',
          right: '10%',
          bottom: '10%'
        },
        xAxis: {
        type: 'category',
        data: chartData.statusList?chartData.statusList:[]
      },
        yAxis: {
         type: 'value',
         name: this.unit
        },
        series: [{
          data: chartData.numbers?chartData.numbers:[],
           type: 'bar',
           barWidth: 40
        }]
      })
    }
  }
}
</script>
